/**
 * Created by huachuan on 2016/1/3.
 */
angular.module('meetingservice',[])
  //约局的Service
  .factory('Meeting', function($resource,$rootScope,ENV){

    //访问的地址
    var APIUrl = ENV.api;
    //接收返回值
    var meetingListJson,collectMeetingJson,likeMeetingJson,meetingDetailJson,signInJson,signInPersionListJson;
    var iReleaseListJson,invitedMeListJson,iApplyListJson,pageSize= 4,memberInfo;
    var signOutMeetingJson,dealInviteMeJson,cancelMeetingJson,meetingCurMsgJson;
    var releaseMeetingResponse,meetingCodeImg,currentMeetingInfo;
    var mmWineList=[],invitedMemberList,memberListJson;
    //存储的key
    //获取$resource
    var getActivityResource = $resource(APIUrl+'findMeeting/getActivityByDay.do');
    var likeActivityResource = $resource(APIUrl+'findMeeting/likeActivity.do');
    var collectActivityResource = $resource(APIUrl+'findMeeting/collectActivity.do');
    var getMeetingDetailResource = $resource(APIUrl+'findMeeting/getActivityDetailById.do');
    var signInActivityResource = $resource(APIUrl+'findMeeting/signInActivity.do');
    var getSignUpPersionListResource = $resource(APIUrl+'findMeeting/getSignUpPersionList.do');
    //我的活动的$resource
    var iReleaseActivityResource = $resource(APIUrl+'myactivity/iReleaseActivity.do');
    var invitedMeActivityResource = $resource(APIUrl+'myactivity/invitedMeActivity.do');
    var iApplyActivityResource = $resource(APIUrl+'myactivity/iApplyActivity.do');
    //对约局的处理，取消报名,取消约局，拒绝，同意约局邀请
    var signOutMeetingResource = $resource(APIUrl+'myactivity/signOutMeeting.do');
    var updateInviteMeResource = $resource(APIUrl+'myactivity/updateInviteMe.do');
    var cancelMeetingResource = $resource(APIUrl+'myactivity/cancelActivity.do');
    var getMeetingCurMsgResource = $resource(APIUrl+'findMeeting/getMeetingCurMsg.do');
    //发布约局的$resource
    var releaseMeetingResource = $resource(APIUrl+'releaseMeeting/releaseMeeting.do');
    var updateMeetingResource = $resource(APIUrl+'releaseMeeting/updateMeeting.do');

    var getCurrentMeetingInfoResource = $resource(APIUrl+'findMeeting/getCurrentInfo.do');
    var findMemberListByIdsResource = $resource(APIUrl+'releaseMeeting/findMemberListByIds.do');
    return {
      //根据会员编号的列表获取到会员的信息
      findMemberListByIds:function(memberIdList){
        findMemberListByIdsResource.save({
          memberIdList:memberIdList
        },function(response){
          memberListJson = response.memberList;
          $rootScope.$broadcast('Meeting.findMemberListByIdsUpdated');
        });
      },
      getMemberListJson:function(){
        return memberListJson;
      },
      //保存约局好友列表
      saveMmInvitedList:function(memberList){
        invitedMemberList = memberList;
      },
      getMmInvitedList:function(){
        return invitedMemberList;
      },
      //保存约局的酒款信息
      saveMmWineList:function(wineList){
        mmWineList = wineList;
      },
      getMmWineList:function(){
          return mmWineList;
      },
      getCurrentMeetingInfo:function(searchList){
        getCurrentMeetingInfoResource.save({
          search_list:searchList
        },function(response){
          currentMeetingInfo = response.currentInfo;
          $rootScope.$broadcast('Meeting.getCurrentMeetingInfoUpdated');
        });
      },
      getCurrentInfo:function(){
        return currentMeetingInfo;
      },
      //保存约局的二维码
      setMeetingCodeImg:function(codeImg){
          meetingCodeImg = codeImg;
      },
      getMeetingCodeImg:function(){
        return meetingCodeImg;
      },
      //发布约局
      releaseMeeting:function(meeting){
        releaseMeetingResource.save({
          meeting:meeting
        },function(response){
          releaseMeetingResponse = response;
          $rootScope.$broadcast('Meeting.releaseMeetingResponseUpdated');
        });
      },
      getReleaseMeetingJson:function(){
        return releaseMeetingResponse;
      },
      //更新约局
      updateMeeting:function(meeting){
        updateMeetingResource.save({
          meeting:meeting
        },function(response){
          releaseMeetingResponse = response;
          $rootScope.$broadcast('Meeting.releaseMeetingResponseUpdated');
        });
      },
      //获取约局的最新信息
      getMeetingCurMsg:function(memberId,mmId){
        getMeetingCurMsgResource.save({
          member_id:memberId,
          mm_id:mmId
        },function(response){
          meetingCurMsgJson = response;
          $rootScope.$broadcast('Meeting.meetingCurMsgUpdated');
        });
      },
      getMeetingCurMsgJson:function(){
        return meetingCurMsgJson;
      },
      //取消约局
      cancelMeeting:function(memberId,mmId){
        cancelMeetingResource.save({
          member_id:memberId,
          mm_id:mmId
        },function(response){
          cancelMeetingJson = response;
          $rootScope.$broadcast('Meeting.cancelMeetingUpdated');
        });
      },
      getCancelMeetingJson:function(){
        return cancelMeetingJson;
      },
      //对邀请我的活动进行处理
      updateInviteMe:function(memberId,mmId,mmiState){
        updateInviteMeResource.save({
          member_id:memberId,
          mm_id:mmId,
          mmi_state:mmiState
        },function(response){
          dealInviteMeJson = response;
          $rootScope.$broadcast('Meeting.dealInviteMeMeetingUpdated');
        });
      },
      getDealInviteMeJson:function(){
        return dealInviteMeJson;
      },
      //取消报名该约局
      signOutMeeting:function(memberId,mmId){
          signOutMeetingResource.save({
            member_id:memberId,
            mm_id:mmId
          },function(response){
            signOutMeetingJson = response;
            $rootScope.$broadcast('Meeting.signOutMeetingUpdated');
          });
      },
      getSignOutMeetingJson:function(){
        return signOutMeetingJson;
      },
      //获取我发起的约局
      getIReleaseList:function(memberId){
        var hasNextPage = true;
        iReleaseActivityResource.save({
          member_id:memberId,
          pageIndex:1,
          pageSize:pageSize
        },function(response){
          if(response.meetingList.length<pageSize){
            hasNextPage = false;
          }
          memberInfo = response.memberInfo;
          iReleaseListJson = {
            hasNextPage:hasNextPage,
            nextPage:2,
            data:response.meetingList
          };
          $rootScope.$broadcast('Meeting.iReleaseActivityUpdated');
        });
      },
      //加载更多我发起的活动
      getMoreIReleaseList:function(memberId){
        if(iReleaseListJson===undefined){
          return false;
        }
        var hasNextPage=iReleaseListJson.hasNextPage;
        var nextPage=iReleaseListJson.nextPage;
        var moreHomeData=iReleaseListJson.data;
        if(!hasNextPage){
          return false;
        }
        iReleaseActivityResource.save({
          member_id:memberId,
          pageIndex:nextPage,
          pageSize:pageSize
        },function(response){
          nextPage++;
          if(response.meetingList.length<pageSize){
            hasNextPage = false;
          }
          moreHomeData = moreHomeData.concat(response.meetingList);
          iReleaseListJson = {
            hasNextPage:hasNextPage,
            nextPage: nextPage,
            data: moreHomeData
          };
          $rootScope.$broadcast('Meeting.iReleaseActivityUpdated');
        });
      },
      getMemberInfo:function(){
        return memberInfo;
      },
      getIReleaseListJson:function(){
        return iReleaseListJson.data;
      },
      //获取邀请我的约局
      getInvitedMeList:function(memberId){
        var hasNextPage = true;
        invitedMeActivityResource.save({
          member_id:memberId,
          pageIndex:1,
          pageSize:pageSize
        },function(response){
          if(response.meetingList.length<pageSize){
            hasNextPage = false;
          }
          invitedMeListJson = {
            hasNextPage:hasNextPage,
            nextPage:2,
            data:response.meetingList
          };
          $rootScope.$broadcast('Meeting.invitedMeActivityUpdated');
        });
      },
      //获取邀请我的更多
      getMoreInvitedMeList:function(memberId){
        if(invitedMeListJson===undefined){
          return false;
        }
        var hasNextPage=invitedMeListJson.hasNextPage;
        var nextPage=invitedMeListJson.nextPage;
        var moreHomeData=invitedMeListJson.data;
        if(!hasNextPage){
          return false;
        }
        invitedMeActivityResource.save({
          member_id:memberId,
          pageIndex:nextPage,
          pageSize:pageSize
        },function(response){
          nextPage++;
          if(response.meetingList.length<pageSize){
            hasNextPage = false;
          }
          moreHomeData = moreHomeData.concat(response.meetingList);
          invitedMeListJson = {
            hasNextPage:hasNextPage,
            nextPage: nextPage,
            data: moreHomeData
          };
          $rootScope.$broadcast('Meeting.invitedMeActivityUpdated');
        });
      },
      getInvitedMeListJson:function(){
        return invitedMeListJson.data;
      },
      //获取已报名的约局
      getIApplyList:function(memberId){
        var hasNextPage = true;
        iApplyActivityResource.save({
          member_id:memberId,
          pageIndex:1,
          pageSize:pageSize
        },function(response){
          if(response.meetingList.length<pageSize){
            hasNextPage = false;
          }
          iApplyListJson = {
            hasNextPage:hasNextPage,
            nextPage:2,
            data:response.meetingList
          };
          $rootScope.$broadcast('Meeting.iApplyActivityUpdated');
        });
      },
      //获取更多已报名的约局
      getMoreIApplyList:function(memberId){
        if(iApplyListJson===undefined){
          return false;
        }
        var hasNextPage=iApplyListJson.hasNextPage;
        var nextPage=iApplyListJson.nextPage;
        var moreHomeData=iApplyListJson.data;
        if(!hasNextPage){
          return false;
        }
        iApplyActivityResource.save({
          member_id:memberId,
          pageIndex:nextPage,
          pageSize:pageSize
        },function(response){
          nextPage++;
          if(response.meetingList.length<pageSize){
            hasNextPage = false;
          }
          moreHomeData = moreHomeData.concat(response.meetingList);
          iApplyListJson = {
            hasNextPage:hasNextPage,
            nextPage: nextPage,
            data: moreHomeData
          };
          $rootScope.$broadcast('Meeting.iApplyActivityUpdated');
        });
      },
      getIApplyListJson:function(){
        return iApplyListJson.data;
      },
      //我发起是否有下一页
      iReleaseHasNextPage:function(){
        if(iReleaseListJson===undefined){
          return false;
        }
        return iReleaseListJson.hasNextPage;
      },
      //邀请我是否有下一页
      invitedMeHasNextPage:function(){
        if(invitedMeListJson===undefined){
          return false;
        }
        return invitedMeListJson.hasNextPage;
      },
      //已报名是否有下一页
      iApplyHasNextPage:function(){
        if(iApplyListJson===undefined){
          return false;
        }
        return iApplyListJson.hasNextPage;
      },
      getSignInPersionList:function(mmId){
        getSignUpPersionListResource.save({
          mm_id:mmId
        },function(response){
          signInPersionListJson = response;
          $rootScope.$broadcast('Meeting.signInPersionListUpdated');
        });
      },
      getSignInPersionListJson:function(){
            return signInPersionListJson;
      },
      //报名该约局
      signInActivity:function(memberId,mmId,username,telephone,weixin){
        signInActivityResource.save({
          mm_id:mmId,
          mma_name:username,
          mma_phone:telephone,
          mma_weixin:weixin,
          member_id:memberId
        },function(response){
          signInJson = response;
          $rootScope.$broadcast('Meeting.signInActivityUpdated');
        });
      },
      getSignInJson:function(){
        return signInJson;
      },
      //根据日期获取当天的所有约局
      getActivityByDay:function(memberId,day,cityId,cityName){
        getActivityResource.save({
          mm_date:day,
          member_id:memberId,
          city_id:cityId,
          city_name:cityName
        },function(response){
            meetingListJson = response;
          $rootScope.$broadcast('Meeting.activityUpdated');
        });
      },
      //返回约局的Json数据
      getActivityListJson:function(){
        return meetingListJson;
      },
      //获取酒款的详情
      getMeetingDetail:function(mmId,memberId){
        getMeetingDetailResource.save({
          mm_id:mmId,
          member_id:memberId
        },function(response){
          meetingDetailJson = response;
          $rootScope.$broadcast('Meeting.meetingDetailUpdated');
        });
      },
      //获取约局详情的Json数据
      getMeetingDetailJson:function(){
        return meetingDetailJson;
      },
      //收藏该约局
      collectMeeting:function(memberId,mmId){
        collectActivityResource.save({
          member_id:memberId,
          mc_obj_id:mmId
        },function(response){
          collectMeetingJson = response;
          $rootScope.$broadcast('Meeting.collectMeetingUpdated');
        });
      },
      //点赞该约局
      likeMeeting:function(memberId,mmId){
        likeActivityResource.save({
          member_id:memberId,
          mm_id:mmId
        },function(response){
          likeMeetingJson = response;
          $rootScope.$broadcast('Meeting.likeMeetingUpdated');
        });
      },
      //返回收藏的信息
      getCollectJson:function(){
        return collectMeetingJson;
      },
      //返回点赞的信息
      getLikeJson:function(){
        return likeMeetingJson;
      }
    }
  })









/**************约局评论的Service**************/
  .factory('MeetingComment', function($resource,$rootScope,ENV) {
    var APIUrl = ENV.api;

    var commentListJson,meetingCommentJson,deleteCommentJson;

    var meetingCommentResource = $resource(APIUrl+"findMeeting/getActivityCommentList.do");
    var publishCommentResource = $resource(APIUrl+"findMeeting/publishComment.do");
    var deleteMeetingCommentResource =  $resource(APIUrl+'findMeeting/delMeetingComment.do');
    //获取的约局的评论列表
    return {
      //删除评论
      deleteComment:function(mmcId,memberId,mmId){
        deleteMeetingCommentResource.save({
          mmc_id:mmcId,
          member_id:memberId,
          mm_id:mmId
        },function(response){
          deleteCommentJson = response;
          $rootScope.$broadcast('Meeting.deleteCommentUpdated');
        });
      },
      getDelCommentJson:function(){
        return deleteCommentJson;
      },
      getCommentList:function(mmId,memberId){
        meetingCommentResource.save({
          mm_id:mmId,
          member_id:memberId
        },function(response){
          commentListJson = response;
          $rootScope.$broadcast('Meeting.CommentListUpdated');
        })
      },
      //发表评论
      publishComment:function(mmId,memberId,newComment){
        publishCommentResource.save({
          member_id:memberId,
          mm_id:mmId,
          mmc_content:newComment
        },function(response){
          meetingCommentJson = response;
          $rootScope.$broadcast('Meeting.publishCommentUpdated');
        });
      },
      getCommentJson:function(){
        return commentListJson;
      },
      //获取到新增的评论内容
      getNewComment:function(){
        return meetingCommentJson;
      }
    }
  })
;




